package com.simplesoft.accountbook;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Settings extends ListActivity implements OnItemClickListener, OnClickListener {
	private SimpleAdapter la = null;
	private ArrayList<HashMap<String, String>> data = null;
	private String from[] = new String[] {
			"title", "description"
	};
	private int to[] = null; 
	private String setting_titles[] = null;
	private String setting_descriptions[] = null;
	private Dialog fileSelector = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.to = new int[] {
				R.id.textview_setting_title,
				R.id.textview_setting_description
		};
		this.data = new ArrayList<HashMap<String, String>>();
		
		setting_titles = this.getResources().getStringArray(R.array.setting_titles);
		setting_descriptions = this.getResources().getStringArray(R.array.setting_descriptions);
		
		// Initialize items of settings.
		for (int i = 0; i < setting_titles.length; i++) {
			HashMap<String, String> item = new HashMap<String, String>();
			item.put(this.from[0], setting_titles[i]);
			item.put(this.from[1], setting_descriptions[i]);
			this.data.add(item);
		}
		
		this.la = new SimpleAdapter(this, this.data, R.layout.setting_item, this.from, this.to);
		this.setListAdapter(this.la);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		if (checkSDCard() != 0) {
			Toast.makeText(this, this.getText(R.string.str_sdcard_is_not_available), Toast.LENGTH_SHORT).show();
			return;
		}
		
		switch (position) {
		case 0:
			//restore
			if (fileSelector == null) {
				fileSelector = new Dialog(this);
				fileSelector.setContentView(R.layout.file_selection_dialog);
				fileSelector.setTitle(getString(R.string.str_select_a_backup));
				((ListView)fileSelector.findViewById(R.id.listview_fileSelector_fileList)).setOnItemClickListener(this);
				((Button)fileSelector.findViewById(R.id.btn_fileSelector_cancel)).setOnClickListener(this);
			} 
			//Get a list of files in SD card.		
			File sdcard = Environment.getExternalStorageDirectory();
			String fNames[] = sdcard.list(new FilenameFilter() {
				
				@Override
				public boolean accept(File dir, String filename) {
					File f = new File(dir, filename);
					if (filename.endsWith(".db") && f.isFile()) {
						return true;
					} else {
						return false;
					}
				}
			});
			ArrayAdapter<String> aa = new ArrayAdapter<String>(
					this,
					R.layout.file_selection_dialog_item,
					R.id.textview_fileSelector_listItem,
					fNames);
			((ListView)fileSelector.findViewById(R.id.listview_fileSelector_fileList)).setAdapter(aa);
			fileSelector.show();
			break;
		case 1:
			//backup
			new ExportDatabaseTask(this).execute();
			break;
		case 2:
			//export csv
			new ExportCSVTask(this).execute();
			break;
		}
	}
	
	private int checkSDCard () {
		String state = Environment.getExternalStorageState();
		
		if (state.equals(Environment.MEDIA_MOUNTED)) {
			return 0; //SD card is okay.
		} else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
			return 1; //SD card is read-only.
		} else {
			return 2; //SD card is not available.
		}
	}

	@Override
	public void onItemClick(AdapterView<?> av, View view, int position, long id) {
		fileSelector.dismiss();		
		String fName = ((TextView)view.findViewById(R.id.textview_fileSelector_listItem)).getText().toString();
		new ImportDatabaseTask(this, fName).execute();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.btn_fileSelector_cancel:
			fileSelector.dismiss();
			break;
		}
	}
}
